    var handlebars = require("hbsfy/runtime");
    var $ = require("jquery");
    /*支持表达式*/
    handlebars.registerHelper("ifexp", function (v1, operator, v2, options) {
        switch (operator) {
            case "==":
                return (v1 == v2) ? options.fn(this) : options.inverse(this);

            case "!=":
                return (v1 != v2) ? options.fn(this) : options.inverse(this);

            case "===":
                return (v1 === v2) ? options.fn(this) : options.inverse(this);

            case "!==":
                return (v1 !== v2) ? options.fn(this) : options.inverse(this);

            case "&&":
                return (v1 && v2) ? options.fn(this) : options.inverse(this);

            case "||":
                return (v1 || v2) ? options.fn(this) : options.inverse(this);

            case "<":
                return (v1 < v2) ? options.fn(this) : options.inverse(this);

            case "<=":
                return (v1 <= v2) ? options.fn(this) : options.inverse(this);

            case ">":
                return (v1 > v2) ? options.fn(this) : options.inverse(this);

            case ">=":
                return (v1 >= v2) ? options.fn(this) : options.inverse(this);

            default:
                return eval("" + v1 + operator + v2) ? options.fn(this) : options.inverse(this);
        }
    });
    /*下标加1*/
    handlebars.registerHelper("plusone", function (index) {
        return index + 1;
    });
    /*切换成分*/
    handlebars.registerHelper("interMoney", function (money) {
        return money.toFixed(2);
    });
    /*性别转换成中文*/
    handlebars.registerHelper("transfromGender", function (gender) {
        if (gender == 0) return '其他';
        if (gender == 1) return '男';
        if (gender == 2) return '女'
    });
    /*日期转换成中文*/
    handlebars.registerHelper("transfromTime", function (day, time) {
        var day_zhcn = {sun: "周日", sat: "周六", fri: "周五", thu: "周四", wed: "周三", tue: "周二", mon: "周一"}
        if (time == 1) {
            return day_zhcn[day] + ":" + "上午";
        } else if (time == 2) {
            return day_zhcn[day] + ":" + "下午";
        } else if (time == 3) {
            return day_zhcn[day] + ":" + "全天";
        }

    });
    /*时间格式化*/
    handlebars.registerHelper("format", function (fmt, time) {
        var scope = new Date(parseInt(time));
        var o = {
            "M+": scope.getMonth() + 1, //月份
            "d+": scope.getDate(), //日
            "h+": scope.getHours(), //小时
            "m+": scope.getMinutes(), //分
            "s+": scope.getSeconds(), //秒
            "q+": Math.floor((scope.getMonth() + 3) / 3), //季度
            "S": scope.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (scope.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

        if (time == null) {
            fmt = "";
        }
        return fmt;
    });
    /*时间格式化_当天显示“时-分”，今年非当天显示“月-日 时-分，本年之前的显示“年-月-日 时+分”*/
    handlebars.registerHelper("format2", function (time) {
        var fmt = '';
        var scope = new Date(parseInt(time));
        var o = {
            "M+": scope.getMonth() + 1, //月份
            "d+": scope.getDate(), //日
            "h+": scope.getHours(), //小时
            "m+": scope.getMinutes(), //分
            "s+": scope.getSeconds(), //秒
            "q+": Math.floor((scope.getMonth() + 3) / 3), //季度
            "S": scope.getMilliseconds() //毫秒

        };
        var newDate = new Date()
        var n = {
            "Y": newDate.getFullYear(), //年
            "M": newDate.getMonth() + 1, //月份
            "d": newDate.getDate(), //日
        }
        if(n.Y == scope.getFullYear()) {

            if((n.M == (scope.getMonth() + 1))&&(n.d == scope.getDate())) {
                fmt = "hh:mm";
            }else{
                fmt = "MM-dd hh:mm";
            }
        }else{
            fmt = "yyyy-MM-dd hh:mm";
        }

        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (scope.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

        if (time == null) {
            fmt = "";
        }
        return fmt;
    });


    handlebars.registerHelper("logogram", function (v) {
        var data;
        if (v) {
            data = "..." + v.substr(v.length - 5, 5);
        } else {
            data = "";
        }
        return data
    });

    handlebars.registerHelper("osdelete", function (v1, v2, options) {
        var _os;
        if (v1.length == 0 && v2 == null) {
            _os = false;
        } else {
            _os = true;
        }
        return _os ? options.fn(this) : options.inverse(this);
    });

    handlebars.registerHelper("role_authority", function (v1, options) {
        if (v1.length == 0) {
            _os = false;
        } else {
            _os = true;
        }
        return _os ? options.fn(this) : options.inverse(this);
    });


    handlebars.registerHelper("formatDocumentNo", function (documentNo, type) {
        var fmt;
        if (documentNo != null && !type) {
            fmt = documentNo.substr(0, 3) + "******" + documentNo.substr(documentNo.length - 4, 4)
        } else if (documentNo != null && type) {
            if (type == 1) {
                fmt = documentNo.substr(0, 3) + "******" + documentNo.substr(documentNo.length - 4, 4)
            }
            if (type == 2 || type == 3) {
                fmt = documentNo.substr(0, 3) + "******" + documentNo.substr(documentNo.length - 2, 2)
            }
        } else {
            fmt = "";
        }
        return fmt;
    });
    handlebars.registerHelper("formatAnswerNo", function (no) {
        var fmt;
        if (no != null) {
            if (no == 0) {
                fmt = "A";
            } else if (no == 1) {
                fmt = "B";
            } else if (no == 2) {
                fmt = "C";
            } else if (no == 3) {
                fmt = "D";
            } else if (no == 4) {
                fmt = "E";
            } else if (no == 5) {
                fmt = "F";
            } else if (no == 6) {
                fmt = "G";
            } else if (no == 7) {
                fmt = "H";
            } else if (no == 8) {
                fmt = "I";
            } else if (no == 9) {
                fmt = "J";
            }
        }
        return fmt;
    });

    handlebars.registerHelper("formatIndex", function (no) {
        var fmt;
        if (no != null) {
            fmt = no + 1;
        }
        return fmt;
    });

    handlebars.registerHelper("formatDecodeURI", function (documentNo) {
        var fmt;
        if (documentNo != null) {
            fmt = decodeURIComponent(documentNo);
        } else {
            fmt = "";
        }
        return fmt;
    });


    handlebars.registerHelper("createHTML", function (text) {
        return text;
    });

    handlebars.registerHelper("formatMobile", function (documentNo) {
        var fmt;
        if (documentNo != null) {
            fmt = documentNo.substr(0, 3) + "****" + documentNo.substr(documentNo.length - 4, 4)
        } else {
            fmt = "";
        }
        return fmt;
    });

    handlebars.registerHelper("formatLength", function (name, length, has) {
        var fmt;
        if (name && name.length > length) {
            fmt = name.substr(0, length)
            if (has) {
                fmt += '...'
            }
        } else {
            fmt = name;
        }
        return fmt;
    });
    
    handlebars.registerHelper("cutOut", function (text) {
        var fmt = "";
        if (text && text.length > 0) {
        	var _textSplit = text.split(",");
        	if(_textSplit.length>0){
        		for(var i = 0;i<_textSplit.length;i++){
        			if(i<3){
        				if(i==2){
        					fmt += _textSplit[i]+"....."
        				}else{
        					fmt += _textSplit[i]+","
        				}
        				
        			}
        		}
        	}else{
        		fmt = text;
        	}
        } else {
            fmt = text;
        }
        return fmt;
    });



    handlebars.registerHelper("formatParseFloat", function (documentNo) {
        var fmt;
        if (documentNo != null) {
            fmt = parseFloat(documentNo);
        } else {
            fmt = "";
        }
        return fmt;
    });

    handlebars.registerHelper("formatListOne", function (documentNo) {
        var fmt;
        if (documentNo && documentNo.length) {
            fmt = '《'+documentNo[0]+'保险》'+ (documentNo.length > 1 ? '等产品': '') +'信息更新，请及时更新！'
        } else {
            fmt = "";
        }
        return fmt;
    });

    handlebars.registerHelper("checkProperty", function (obj, field, options) {
        return obj.hasOwnProperty(field) ? options.fn(this) : options.inverse(this);
    });
    handlebars.registerHelper("getField", function (obj, field) {
        var data = obj[field.propName];
        if(!data){
            data = "-";
            return
        }
        if (field.propName === 'startTime' || field.propName === 'endTime') {
            data = data && data.substr(0, 16);
        }
        

        return data;
    });
    handlebars.registerHelper("checkNone", function (value, def) {
        return value ? value : def
    });

    handlebars.registerHelper("JSONParse", function (value,name) {
        return value ? JSON.parse(value)[name] : value
    });

    /*限制字段长度*/
    handlebars.registerHelper("limitLength", function (value,value2,length) {
        if(value) {
           return cutstr(value, length)
        }else if(value2){
           return cutstr(value2, length)
        }

        function cutstr(str, len) {
            var str_length = 0;
            var str_len = 0;
            str_cut = new String();
            str_len = str.length;
            for (var i = 0; i < str_len; i++) {
                a = str.charAt(i);
                str_length++;
                if (escape(a).length > 4) {
                    //中文字符的长度经编码之后大于4
                    str_length++;
                }
                str_cut = str_cut.concat(a);
                if (str_length >= len) {
                    str_cut = str_cut.concat("...");
                    return str_cut;
                }
            }
            //如果给定字符串小于指定长度，则返回源字符串；
            if (str_length < len) {
                return str;
            }
        }
    });

